package com.software.task.controller;

import com.software.task.pojo.GradeDTO;
import com.software.task.pojo.Homework;
import com.software.task.pojo.PageBean;
import com.software.task.pojo.Result;
import com.software.task.service.HomeworkService;
import jakarta.annotation.Resource;

import org.springframework.web.bind.annotation.*;
/**
 * @author 浦慧慧
 * @since 2024/12/15 11.34
 * @version 1.0
 */
@RestController
@RequestMapping("/homeworks")
public class HomeworkController {

    /**
     * Autowired注解用于将HomeworkService注入到HomeworkController中
     */
    @Resource
    private HomeworkService homeworkService;

    /**
     * PostMapping 注解用于处理添加作业请求
     * @param homework 作业实体类
     * @return 返回Result对象，包含操作结果
     */
    @PostMapping
    public Result<?> add(@RequestBody Homework homework) {
        homeworkService.add(homework);
        return Result.success();
    }

    /**
     * DeleteMapping注解用于处理删除作业请求
     */
    @GetMapping
    public Result<PageBean<Homework>> getAllSubmittedSubjects(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) Integer teacherId) {
        PageBean<Homework> pageBean = homeworkService.getAllSubmittedSubjects(pageNum, pageSize,teacherId);
        return Result.success(pageBean);
    }

    /**
     * PatchMapping注解用于处理评分作业请求
     */
    @PatchMapping("/{id}")
    public Result<?> gradeHomework(@PathVariable Integer id, @RequestBody GradeDTO gradeDTO) {
        Integer grade = gradeDTO.getGrade();
        if (grade == null) {
            return Result.error("评分不能为空");
        }
        Integer updatedGrade = homeworkService.gradeHomework(id, grade);
        return Result.success(updatedGrade);
    }

}